import { BaseModel } from './BaseModel.js';
import { dataFiles } from '../config/database.js';

const defaultPermissions = [
    // 学生管理权限
    { id: 1, name: '查看学生', code: 'student:read', resource: 'student', action: 'read', type: 'api', description: '查看学生列表和详情' },
    { id: 2, name: '创建学生', code: 'student:create', resource: 'student', action: 'create', type: 'api', description: '创建新学生' },
    { id: 3, name: '更新学生', code: 'student:update', resource: 'student', action: 'update', type: 'api', description: '更新学生信息' },
    { id: 4, name: '删除学生', code: 'student:delete', resource: 'student', action: 'delete', type: 'api', description: '删除学生' },
    { id: 5, name: '导出学生', code: 'student:export', resource: 'student', action: 'export', type: 'button', description: '导出学生数据' },
    { id: 6, name: '导入学生', code: 'student:import', resource: 'student', action: 'import', type: 'button', description: '导入学生数据' },

    // 教师管理权限
    { id: 7, name: '查看教师', code: 'teacher:read', resource: 'teacher', action: 'read', type: 'api', description: '查看教师列表和详情' },
    { id: 8, name: '创建教师', code: 'teacher:create', resource: 'teacher', action: 'create', type: 'api', description: '创建新教师' },
    { id: 9, name: '更新教师', code: 'teacher:update', resource: 'teacher', action: 'update', type: 'api', description: '更新教师信息' },
    { id: 10, name: '删除教师', code: 'teacher:delete', resource: 'teacher', action: 'delete', type: 'api', description: '删除教师' },

    // 成绩管理权限
    { id: 11, name: '查看成绩', code: 'grade:read', resource: 'grade', action: 'read', type: 'api', description: '查看学生成绩' },
    { id: 12, name: '管理成绩', code: 'grade:manage', resource: 'grade', action: 'manage', type: 'api', description: '录入和管理成绩' },

    // 考勤管理权限
    { id: 13, name: '查看考勤', code: 'attendance:read', resource: 'attendance', action: 'read', type: 'api', description: '查看考勤记录' },
    { id: 14, name: '管理考勤', code: 'attendance:manage', resource: 'attendance', action: 'manage', type: 'api', description: '录入和管理考勤' },

    // 班级管理权限
    { id: 15, name: '管理班级', code: 'class:manage', resource: 'class', action: 'manage', type: 'api', description: '管理班级信息' },

    // 仪表盘权限
    { id: 16, name: '查看仪表盘', code: 'dashboard:view', resource: 'dashboard', action: 'view', type: 'menu', description: '查看系统仪表盘' },

    // 用户管理权限
    { id: 17, name: '管理用户', code: 'user:manage', resource: 'user', action: 'manage', type: 'api', description: '管理用户账号' },

    // 系统设置权限
    { id: 18, name: '系统设置', code: 'system:config', resource: 'system', action: 'config', type: 'menu', description: '系统配置管理' },

    // 角色权限管理
    { id: 19, name: '管理角色', code: 'role:manage', resource: 'role', action: 'manage', type: 'api', description: '管理角色和权限' },

    // 视频会议权限
    { id: 20, name: '使用视频会议', code: 'video:use', resource: 'video', action: 'use', type: 'menu', description: '使用视频会议功能' },
    { id: 21, name: '创建会议', code: 'video:create', resource: 'video', action: 'create', type: 'button', description: '创建视频会议' }
];

export class Permission extends BaseModel {
    constructor() {
        super(dataFiles.permissions, defaultPermissions);
    }

    findByCode(code) {
        return this.findOne({ code });
    }

    findByResource(resource) {
        return this.findAll().filter(permission => permission.resource === resource);
    }

    findByType(type) {
        return this.findAll().filter(permission => permission.type === type);
    }

    findByResourceAndAction(resource, action) {
        return this.findOne({ resource, action });
    }
}

